<?php
// +----------------------------------------------------------------------
// | RXThinkCMF敏捷开发框架 [ 赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2022 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | Licensed LGPL-3.0 RXThinkCMF并不是自由软件，未经许可禁止去掉相关版权
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: @牧羊人 团队荣誉出品
// +----------------------------------------------------------------------
// | 版权和免责声明:
// | 本团队对该软件框架产品拥有知识产权（包括但不限于商标权、专利权、著作权、商业秘密等）
// | 均受到相关法律法规的保护，任何个人、组织和单位不得在未经本团队书面授权的情况下对所授权
// | 软件框架产品本身申请相关的知识产权，禁止用于任何违法、侵害他人合法权益等恶意的行为，禁
// | 止用于任何违反我国法律法规的一切项目研发，任何个人、组织和单位用于项目研发而产生的任何
// | 意外、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、
// | 附带或衍生的损失等)，本团队不承担任何法律责任，本软件框架禁止任何单位和个人、组织用于
// | 任何违法、侵害他人合法利益等恶意的行为，如有发现违规、违法的犯罪行为，本团队将无条件配
// | 合公安机关调查取证同时保留一切以法律手段起诉的权利，本软件框架只能用于公司和个人内部的
// | 法律所允许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\adminmini\controller;

use app\admin\model\Example;
use app\adminmini\service\MenuService;
use app\adminmini\service\UserService;
use think\facade\Db;

/**
 * 系统主页-控制器
 * @author 牧羊人
 * @since 2020/11/14
 * Class Index
 * @package app\admin\controller
 */
class Index extends Backend
{

    /**
     * 初始化
     * @return array|void
     * @since 2021/1/8
     * @author 牧羊人
     */
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub

    }


    public function Statistics()
    {

        $param = request()->param();


        $where = [];


        $count = Db::name('mini_room_list_distribution')->where('direct_id', $this->userId())->where('store_id', '>', 0)->where($where)->count();
        $data['self_room'] = $count;

        // 收益
        $price = Db::name('distribution_price')->where('store_id', $this->userId())->where('second_id', '=',0)->where($where)->sum('price');
        $data['totalPrice'] = $price;
        // 二级门店统计


        $count = Db::name('mini_room_list_distribution')->where('indirect_id', $this->userId())->where($where)->count();
        $data['second'] = $count;

        $price = Db::name('distribution_price')->where('store_id', $this->userId())->where('second_id', '<>',0)->where('leve_id', '=',0)->where($where)->sum('price');
        $data['secondPrice'] = $price;
        // 3级团队统计

        $count = Db::name('mini_room_list_distribution')->where('Introduction_id', $this->userId())->where($where)->count();
        $data['level'] = $count;

        $price = Db::name('distribution_price')->where('leve_id', $this->userId())->where('second_id', '<>',0)->where('leve_id', '<>',0)->where($where)->sum('price');
        $data['levelPrice'] = $price;

        // 区间收益
        $pricequ = Db::name('distribution_price')->where('is_long', $this->userId())->where($where)->sum('price');

        $data['quPrice'] = $pricequ;

        // 团队收益
        $data['teamprice'] = round($data['levelPrice'] + $data['secondPrice'] + $data['totalPrice'] + $pricequ,2);

        // 店铺收益

        $sotregoodsPirce = Db::name('customer_integral_product_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($where)->sum('pay_money');

//充值
        $sotrerechargePirce = Db::name('customer_recharge_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($where)->sum('price');
        //团购卷订单
        $couponPirce = Db::name('customer_coupon_order')->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($where)->sum('price');
        $cardcoupon = Db::name('customer_cardcoupon_order')->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($where)->sum('price');

        //订单
        $sotreorderPirce = Db::name('customer_order')->where('pay_type', 1)->where('status', '<>', 3)->where('status', '>', 0)->where('store_id', $this->userId())->where($where)->sum('pay_price');
        $sotreorderPircej = Db::name('customer_order')->where('status', '<>', 3)->where('status', '>', 0)->where('store_id', $this->userId())->where($where)->sum('jia_price');

        //抖音美团加时
        $dmPirce = Db::name('customer_order')->whereIn('pay_type', ['2','3','4','5','6'])->where('status', '>', 0)->where('pay_price', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($where)->sum('pay_price');
        $orderX = Db::name('customer_renewal_order')->where('pay_type', 1)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($where)->sum('pay_price');


        $sotreorderPirce=$sotreorderPirce-$sotreorderPircej+$dmPirce+$orderX;
        $sotreseatPirce = Db::name('customer_seat_order')->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($where)->sum('price');


        $shopprice =  round($sotregoodsPirce + $sotrerechargePirce+$couponPirce+$cardcoupon + $sotreorderPirce + $sotreseatPirce * 0.15,2);

        $data['shopprice'] = $shopprice;


        // 日收入
        $add_time = strtotime(date('Y-m-d', time()));

        $end_time = $add_time + 86400;

        $wheres[] = ['create_time', '>=', $add_time];
        $wheres[] = ['create_time', '<=', $end_time];

        $sotregoodsPirce = Db::name('customer_integral_product_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($wheres)->sum('pay_money');
        $sotrerechargePirce = Db::name('customer_recharge_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($wheres)->sum('price');
        $couponPirce = Db::name('customer_coupon_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($wheres)->sum('price');
        $cardcoupon = Db::name('customer_cardcoupon_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($wheres)->sum('price');

        $sotreorderPirce = Db::name('customer_order')->where('pay_type', 1)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($wheres)->sum('pay_price');



        $sotreorderPircej = Db::name('customer_order')->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($wheres)->sum('jia_price');

        //抖音美团加时
        $dmPirce = Db::name('customer_order')->whereIn('pay_type', ['2','3','4','5','6'])->where('pay_price', '>', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($wheres)->sum('pay_price');
        $orderX = Db::name('customer_renewal_order')->where('pay_type', 1)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($wheres)->sum('pay_price');

        $sotreorderPirce=$sotreorderPirce-$sotreorderPircej+$dmPirce+$orderX;
        $sotreseatPirce = Db::name('customer_seat_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($wheres)->sum('price');

        $shopprice = $sotregoodsPirce + $sotrerechargePirce+$couponPirce+$cardcoupon + $sotreorderPirce + $sotreseatPirce * 0.15;
        $data['rieshopprice'] = round($shopprice,2);


        // 订单
        $sotregoodsPirce = Db::name('customer_integral_product_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($wheres)->count();


        $sotrerechargePirce = Db::name('customer_recharge_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($wheres)->count();
        $couponPirce = Db::name('customer_coupon_order')->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($wheres)->count();
        $cardcoupon = Db::name('customer_cardcoupon_order')->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($wheres)->count();

        $sotreorderPirce = Db::name('customer_order')->where('pay_type', 1)->where('status', '<>', 3)->where('status', '>', 0)->where('store_id', $this->userId())->where($wheres)->count();
        $dmPirce = Db::name('customer_order')->whereIn('pay_type', ['2','3','4','5','6'])->where('pay_price', '>', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($wheres)->count();
        $orderX = Db::name('customer_renewal_order')->where('pay_type', 1)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($wheres)->count();
        $sotreorderPirce=$sotreorderPirce+$dmPirce+$orderX;
        $sotreseatPirce = Db::name('customer_seat_order')->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($wheres)->count();
        $shopprice = $sotregoodsPirce + $sotrerechargePirce+$couponPirce+$cardcoupon + $sotreorderPirce + $sotreseatPirce;
        $data['order_ri'] = $shopprice;

        // 月收入
        $add_time = mktime(0, 0, 0, date('m'), 1, date('Y'));
        $tia = date("t", strtotime(date('Y-m-d'))) + 1;
        $wheres = array();
        $end_time = $add_time + 86400 * $tia * 86400;
        $whereyu[] = ['create_time', '>=', $add_time];
        $whereyu[] = ['create_time', '<=', $end_time];

        $sotregoodsPirce = Db::name('customer_integral_product_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($whereyu)->sum('pay_money');


        $sotrerechargePirce = Db::name('customer_recharge_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($whereyu)->sum('price');
        $couponPirce = Db::name('customer_coupon_order')->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($whereyu)->sum('price');
        $cardcoupon = Db::name('customer_cardcoupon_order')->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($whereyu)->sum('price');

        $sotreorderPirce = Db::name('customer_order')->where('status', '<>', 3)->where('pay_type', 1)->where('status', '>', 0)->where('store_id', $this->userId())->where($whereyu)->sum('pay_price');
        $sotreorderPircej = Db::name('customer_order')->where('status', '<>', 3)->where('status', '>', 0)->where('store_id', $this->userId())->where($whereyu)->sum('jia_price');

        //抖音美团加时
        $dmPirce = Db::name('customer_order')->whereIn('pay_type', ['2','3','4','5','6'])->where('pay_price', '>', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($whereyu)->sum('pay_price');
        $orderX = Db::name('customer_renewal_order')->where('pay_type', 1)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($whereyu)->sum('pay_price');

        $sotreorderPirce=$sotreorderPirce+$dmPirce-$sotreorderPircej+$orderX;
        $sotreseatPirce = Db::name('customer_seat_order')->where('status', '<>', 3)->where('status', '>', 0)->where('store_id', $this->userId())->where($whereyu)->sum('price');


        $shopprice = $sotregoodsPirce + $sotrerechargePirce+$couponPirce+$cardcoupon + $sotreorderPirce + $sotreseatPirce * 0.15;
        $data['yueshopprice'] = round($shopprice - $price,2);
        $sotregoodsPirce = Db::name('customer_integral_product_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($whereyu)->count();
        $sotrerechargePirce = Db::name('customer_recharge_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($whereyu)->count();
        $couponPirce = Db::name('customer_coupon_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($whereyu)->count();
        $cardcoupon = Db::name('customer_cardcoupon_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($whereyu)->count();

        $sotreorderPirce = Db::name('customer_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($whereyu)->count();
        $sotreseatPirce = Db::name('customer_seat_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($whereyu)->count();
        $shopprice = $sotregoodsPirce + $sotrerechargePirce+$couponPirce+$cardcoupon + $sotreorderPirce + $sotreseatPirce;
        $data['order_yue'] = $shopprice;
        //$sotregoodsPirce = Db::name('customer_integral_product_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($where)->sum('pay_money');
        $sotrerechargePirce = Db::name('customer_recharge_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($where)->sum('price');
        $couponPirce = Db::name('customer_coupon_order')->where('status', '>', 0)->where('status', '<>', 3)->where('status', '<>', 3)->where('store_id', $this->userId())->where($where)->sum('price');
        $cardcoupon = Db::name('customer_cardcoupon_order')->where('status', '>', 0)->where('status', '<>', 3)->where('status', '<>', 3)->where('store_id', $this->userId())->where($where)->sum('price');

        $sotreorderPirce = Db::name('customer_order')->where('pay_type', 1)->where('status', '<>', 3)->where('status', '>', 0)->where('store_id', $this->userId())->where($where)->sum('pay_price');
        //抖音美团加时
        $dmPirce = Db::name('customer_order')->whereIn('pay_type', ['2','3','4','5','6'])->where('pay_price', '>', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($where)->sum('pay_price');
        $sotreorderPirce=$sotreorderPirce+$dmPirce;
        $sotreseatPirce = Db::name('customer_seat_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($where)->sum('price');

        $shopprice = $sotregoodsPirce + $sotrerechargePirce +$couponPirce+$cardcoupon+ $sotreorderPirce + $sotreseatPirce * 0.15;

        $pricede = 0;
        $pricess = 0;//优惠卷
        $list = Db::name('customer_renewal_order')->where('status', 1)->where('pay_type', 1)->where('store_id', $this->userId())->select();

        if (count($list) > 0) {
            foreach ($list as $i => $v) {
                $order = Db::name('customer_order')->where('id', $v['order_renewal_id'])->find();


                if (!$order) {


                    $pricede += $v['pay_price'];
                } else {
                    $pri = 0;
                    $pri1 = 0;
                    if ($order['status'] != 3) {

                        if ($v['coupon_id'] > 0) {
                            $userCoupon = Db::name('mini_user_coupon')->where('id', $v['coupon_id'])->find();
                            if ($userCoupon) {
                                $coupon = Db::name('coupon')->where('id', $userCoupon['coupon_id'])->find();
                                if ($coupon) {
                                    $pri = $coupon['price'];
                                }

                            }

                        }
                        if ($order['coupon_id'] > 0) {
                            $userCoupon = Db::name('mini_user_coupon')->where('id', $order['coupon_id'])->find();
                            if ($userCoupon) {
                                $coupon = Db::name('coupon')->where('id', $userCoupon['coupon_id'])->find();
                                if ($coupon) {
                                    $pri1 = $coupon['price'];
                                }
                            }


                        }
                        $price = $v['price'] + $order['price'] - $pri - $pri1;
                        if ($price > $order['pay_price']) {
                            $pricess += $price - $order['pay_price'];
                        }
                    }

                }
            }
        }


        //  $data['shopprice'] = round($shopprice + $pricede + $pricess, 2);
        $shopprice = $sotregoodsPirce + $sotrerechargePirce+$couponPirce + $sotreorderPirce;
        $data['zoeshopprice'] = $data['shopprice'];
        $sotregoodsPirce = Db::name('customer_integral_product_order')->where('status', '>', 0)->where('store_id', $this->userId())->count();
        $sotrerechargePirce = Db::name('customer_recharge_order')->where('status', '>', 0)->where('store_id', $this->userId())->count();
        $couponPirce = Db::name('customer_coupon_order')->where('status', '>', 0)->where('store_id', $this->userId())->count();
        $cardcoupon = Db::name('customer_cardcoupon_order')->where('status', '>', 0)->where('store_id', $this->userId())->count();

        $sotreorderPirce = Db::name('customer_order')->where('status', '>', 0)->where('store_id', $this->userId())->count();
        $sotreseatPirce = Db::name('customer_seat_order')->where('status', '>', 0)->where('store_id', $this->userId())->count();
        $shopprice = $sotregoodsPirce + $sotrerechargePirce+$couponPirce+$cardcoupon + $sotreorderPirce + $sotreseatPirce;
        $data['order_tount'] = $shopprice;
        return message('成功', true, $data);
    }


    public function mFristAndLast($y = "", $m = "")
    {
        if ($y == "") $y = date("Y");
        if ($m == "") $m = date("m");
        $m = sprintf("%02d", intval($m));
        $y = str_pad(intval($y), 4, "0", STR_PAD_RIGHT);

        $m > 12 || $m < 1 ? $m = 1 : $m = $m;
        $firstday = strtotime($y . $m . "01000000");
        $firstdaystr = date("Y-m-01", $firstday);
        $lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));
        return array("firstday" => $firstday, "lastday" => $lastday);
    }

    /**
     * 获取菜单列表
     * @author 牧羊人
     * @since 2020/11/14
     */
    public function getMenuList()
    {
//        $listu = Db::name('customer_user')
//            ->alias('cu')
//            ->join('customer_user_role cur', 'cu.id=cur.user_id')
//            ->group('cu.room_id')
//            ->field('cu.id,cur.user_id,cur.role_id,cu.room_id')
//            ->order('cu.room_id','desc')
//            ->select()->toArray();
//        foreach ($listu as $i => $v) {
//            $role_menu=[];
//            $role_menu = array(
//                'role_id' => $v['role_id'],
//                'menu_id' => 2199,
//            );
//                Db::name('customer_role_menu')->save($role_menu);
//        }
//       // echo Db::getlastsql();exit();
//        exit();

        $menuService = new MenuService();
        $result = $menuService->getPermissionList($this->userId);
        return $result;
    }


    public function getIndexDataList()
    {
        $param = request()->param();
        if (!isset($param['add_time'])) {
            $add_time = date('Y-m-d', strtotime(date('Y-m-d', time())) - 7 * 86400);
            $end_time = date('Y-m-d', strtotime(date('Y-m-d', time())));
        } else {
            $add_time = date('Y-m-d', strtotime($param['add_time']));
            $end_time = date('Y-m-d', strtotime($param['end_time']) + 86400);
        }
        $ti = intval((strtotime($end_time) - strtotime($add_time)) / 86400);
        $data = array();
        $data['order'] = array();
        $wheres [] = ['pay_time', '>=', strtotime($add_time)];
        $wheres [] = ['pay_time', '<=', strtotime($end_time)];


        for ($i = 0; $i < $ti; $i++) {

            $wheres = [];
            $add = strtotime($add_time) + $i * 86400;
            $end = strtotime($add_time) + 86400 + $i * 86400;
            $wheres [] = ['pay_time', '>=', $add];
            $wheres [] = ['pay_time', '<=', $end];

//普通单微信
            $sotreseatPirce = Db::name('customer_order')->where('pay_type', 1)->where('pay_price', '>', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($wheres)->sum('pay_price');

            $sotreseatPircej = Db::name('customer_order')->where('pay_price', '>', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($wheres)->sum('jia_price');

            //加时单微信
            $renewalPirce = Db::name('customer_renewal_order')->where('pay_type', 1)->where('pay_price', '>', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($wheres)->sum('pay_price');

            $ordersum = Db::name('customer_order')->where('pay_type', 1)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($wheres)->count('*');

            //抖音美团微信
            $dmPirce = Db::name('customer_order')->whereIn('pay_type', ['2','3','4','5','6'])->where('status', '>', 0)->where('pay_price', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($wheres)->sum('pay_price');
            $sotreseatPirce=round($sotreseatPirce+$dmPirce-$sotreseatPircej+$renewalPirce,2);
//充值
            $sotrerechargePirce = Db::name('customer_recharge_order')->where('status', '>', 0)->where('store_id', $this->userId())->where($wheres)->sum('price');
            $couponPirce = Db::name('customer_coupon_order')->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($wheres)->sum('price');
            $cardcoupon = Db::name('customer_cardcoupon_order')->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $this->userId())->where($wheres)->sum('price');

            $data['order'][] = array(
                'create_time' => date('Y-m-d', $add),
                'order_price' => $sotreseatPirce,//正常
                'renewalorder_price'=>$renewalPirce,//加时
                'user_price' => $sotrerechargePirce,//充值
                'coupon_price' => $couponPirce,//团购卷
                'cardcoupon' => $cardcoupon,//周卡
                'ordersum'=>$ordersum,
            );
        }
        $count = 0;
        foreach ($data['order'] as $i => $v) {
            $shopprice = $v['order_price'] + $v['user_price']+$v['coupon_price']+$v['cardcoupon'];
            $count += $shopprice;
        }

        $data['shopprice'] = round($count , 2);
        return message('成功', true, $data);

    }

    /**
     * 获取用户信息
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @since 2020/11/20
     * @author 牧羊人
     */
    public function getUserInfo()
    {
        $userService = new UserService();
        $result = $userService->getUserInfo($this->userId);

        return $result;
    }

    /**
     * 更新个人资料
     * @return mixed
     * @since 2020/11/11
     * @author 牧羊人
     */
    public function updateUserInfo()
    {
        $userService = new UserService();
        $result = $userService->updateUserInfo($this->userId);
        return $result;
    }

    /**
     * 更新密码
     * @return array
     * @throws \think\db\exception\BindParamException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 牧羊人
     * @since 2020/11/15
     */
    public function updatePwd()
    {
        $userService = new UserService();
        $result = $userService->updatePwd($this->userId);
        return $result;
    }

}